package com.ch.clinking.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.ch.clinking.R;
import com.ch.clinking.databinding.ActivityCalculateMakeOrderBinding;
import com.ch.clinking.databinding.ActivityCalculateProfitBinding;

public class CalculateMakeOrderActivity extends AppCompatActivity {

    private ActivityCalculateMakeOrderBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityCalculateMakeOrderBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.calculateMakeOrderCalculateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String calculateTemuStock = binding.calculateTemuStock.getText().toString().trim();
                String calculateOnWay = binding.calculateOnWay.getText().toString().trim();
                String calculateSelfStockPrice = binding.calculateSelfStockPrice.getText().toString().trim();
                String calculateS = binding.calculateS.getText().toString().trim();
                String calculateMakeOrderW = binding.calculateMakeOrderW.getText().toString().trim();
                String calculateMakeOrderD = binding.calculateMakeOrderD.getText().toString().trim();
                String calculateMakeOrderZ = binding.calculateMakeOrderZ.getText().toString().trim();

                if (calculateTemuStock.isEmpty() || calculateOnWay.isEmpty() || calculateSelfStockPrice.isEmpty() || calculateS.isEmpty() || calculateMakeOrderW.isEmpty() || calculateMakeOrderD.isEmpty() || calculateMakeOrderZ.isEmpty()) {

                    Toast.makeText(CalculateMakeOrderActivity.this, "未填写完全", Toast.LENGTH_SHORT).show();
                    return;

                }

                CalculateMakeOrder(Integer.parseInt(calculateTemuStock), Integer.parseInt(calculateOnWay), Integer.parseInt(calculateSelfStockPrice), Double.parseDouble(calculateS), Integer.parseInt(calculateMakeOrderW), Integer.parseInt(calculateMakeOrderD), Integer.parseInt(calculateMakeOrderZ));

            }
        });


    }


    private void CalculateMakeOrder(int temu_stock, int on_way, int self_stock, double S, int W, int D, int ZZ) {

        // S 平均销售数量
        // W = 7 生产周期
        // D = 5 每单补货量的天数
        // ZZ 真正做的数量
        double Num = 0; // 需要下单的数量

        while ((temu_stock + on_way + self_stock + ZZ + Num - S * W) <= 0) {
            Num += D * S;
        }

        if ((temu_stock + on_way + self_stock - S * W) <= 0) {
            binding.calculateMakeOrderUrgent.setText("是");
        } else {
            binding.calculateMakeOrderUrgent.setText("否");
        }


        @SuppressLint("DefaultLocale") String formattedValue = String.format("%.2f", Num);

        binding.calculateMakeOrderMakeOrder.setText(formattedValue);

    }

}